package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.BaseUser;
import ysq.xyg.model.Member;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.BaseUserService;
import ysq.xyg.service.MemberService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(MemberController.class);
    @Resource
    private MemberService memberService;
    @Resource
    private BaseUserService baseUserService;
    private Member member;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/memberList");
    }
    @RequestMapping("/listPageYQ")
    public ModelAndView listPageYQ(HttpServletRequest request, HttpServletResponse response,String menuid){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        return new ModelAndView("/memberListYQ").addObject("baseUserInfo",baseUserInfo);
    }
    @RequestMapping("/listPageCount")
    public ModelAndView listPageCount(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/memberListCount");
    }
    @RequestMapping("/listPageXFCount")
    public ModelAndView listPageXFCount(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/memberListXFCount");
    }
    @RequestMapping(value = "/JsonListXFCount",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonListXFCount(Member member,Page page){
        PageInfo<Member> pageInfo=memberService.pageSelectEntityXFCount(member,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(Member item:pageInfo.getList()){
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("openid",item.getOpenid());
                row.put("money",item.getMoney());
                row.put("name",item.getName());
                row.put("headImage",item.getHeadImage());
                if(StringUtil.isNotEmpty(item.getNickName())){
                    row.put("nickName", BaseUtil.decodeBase64(item.getNickName()));
                }
                row.put("phone",item.getPhone());
                row.put("introductUserId",item.getIntroductUserId());
                row.put("createtime", DateUtil.DateToStr(item.getCreateTime(),DateUtil.DATE_TIME_PATTERN));
                row.put("allMoney",item.getAllMoney());
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }
    @RequestMapping(value = "/JsonListCount",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonListCount(Member member,Page page){
        PageInfo<Member> pageInfo=memberService.pageSelectEntityCount(member,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(Member item:pageInfo.getList()){
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("openid",item.getOpenid());
                row.put("money",item.getMoney());
                row.put("name",item.getName());
                row.put("headImage",item.getHeadImage());
                if(StringUtil.isNotEmpty(item.getNickName())){
                    row.put("nickName", BaseUtil.decodeBase64(item.getNickName()));
                }
                row.put("phone",item.getPhone());
                row.put("introductUserId",item.getIntroductUserId());
                row.put("createtime", DateUtil.DateToStr(item.getCreateTime(),DateUtil.DATE_TIME_PATTERN));
                row.put("num",item.getNum());
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }
    @RequestMapping(value = "/JsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(Member member,Page page){
        PageInfo<Member> pageInfo=memberService.pageInfo(member,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(Member item:pageInfo.getList()){
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("openid",item.getOpenid());
                row.put("money",item.getMoney());
                row.put("name",item.getName());
                row.put("headImage",item.getHeadImage());
                if(StringUtil.isNotEmpty(item.getNickName())){
                    row.put("nickName",BaseUtil.decodeBase64(item.getNickName()));
                }
                row.put("phone",item.getPhone());
                row.put("introductUserId",item.getIntroductUserId());
                row.put("createtime", DateUtil.DateToStr(item.getCreateTime(),DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String id){
        ModelAndView view=new ModelAndView("/memberUpdateOrSave");
        member=memberService.selectByPrimaryKey(id);
        view.addObject("member",member);
        return view;
    }
    @RequestMapping(value = "/JsonSelectEntity")
    public @ResponseBody JSONObject JsonList(String id) throws Exception {
        JSONObject jsonObject=new JSONObject();
        ReturnInfo returnInfo=new ReturnInfo();
        member=memberService.selectByPrimaryKey(id);
        if(member==null){
            returnInfo=new ReturnInfo(BaseConstant.FAIL,"会员ID不存在");
        }else{
            if(StringUtil.isNotEmpty(member.getNickName())){
                member.setNickName(BaseUtil.decodeBase64(member.getNickName()));
            }
            returnInfo.setResult(member);
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }

    /**
     * 设置为收货员
     * 即为user表增加一条数据
     */
    @RequestMapping(value = "/insertUserEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject insertUserEntity(String memberId) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        // 判断是否已设置
        if (StringUtils.isEmpty(memberId)) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, "您传入的会员ID为空");
            jsonObject.put("returnInfo", returnInfo);
            return jsonObject;
        }
        BaseUser baseUserRecord = baseUserService.selectByPrimaryKey(memberId);
        if (baseUserRecord != null) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, "该会员已成为了收货员");
            jsonObject.put("returnInfo", returnInfo);
            return jsonObject;
        }
        Member member = memberService.selectByPrimaryKey(memberId);
        BaseUser baseUser = new BaseUser();
        baseUser.setUserid(memberId);
        if (StringUtils.isEmpty(member.getName())) {
            if(StringUtil.isNotEmpty(member.getNickName())){
                baseUser.setUsername(BaseUtil.decodeBase64(member.getNickName()));
            }
        } else {
            baseUser.setUsername(member.getName());
        }
        baseUser.setUserpassword("123456");
        baseUser.setCreatetime(new Date());
        baseUser.setRemovetag(BaseConstant.REMOVE_N);
        baseUser.setDepartid("153537628190314134182");
        baseUser.setHeadlogo(member.getHeadImage());
        int id = baseUserService.insert(baseUser);
        if(id <= 0){
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/updateFlowNumEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateFlowNumEntity(String memberId,Integer flowNum) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        // 判断是否已设置
        if (StringUtils.isEmpty(memberId)) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, "您传入的会员ID为空");
            jsonObject.put("returnInfo", returnInfo);
            return jsonObject;
        }
        Member member = new Member();
        member.setId(memberId);
        member.setFlowNum(flowNum);
        int id = memberService.updateByPrimaryKeySelective(member);
        if(id <= 0){
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }
}
